package org.come.init;

import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.io.File;

import org.DownLoad.DownloadStart;
import org.come.log.LanderJPanel;
import org.json.JSONObject;

public class UpdatePack
{
    private String version;
    private long length;
    private UpdateView updateView;
    private LanderJPanel landerJPanel;

    public   UpdatePack(final String version, final UpdateView updateView, final long length) {
        this.version = version;
        this.length = length;
        this.updateView = updateView;
        // this.startUpdate();
    }

    UpdatePack(final String version, final LanderJPanel landerJPanel, final long length) {
        this.version = version;
        this.length = length;
        this.landerJPanel = landerJPanel;
        this.startUpdate();
    }
    public  boolean start(){
        if (startUpdate()){
            return true;
        }else return false;
    }
    public  boolean startUpdate() {
        try {
            long start = System.currentTimeMillis();
            //热更下载地址
            // Tool.downloadHttpUrl("http://127.0.0.1:8088/GameServer/update/"+version+".zip", System.getProperty("user.dir")+"/update/UpdatePacks/", version+".zip", updateView, length);
            // Tool.downloadHttpUrl("http://121.5.121.148:8083/GameServer/update/"+version+".zip", System.getProperty("user.dir")+"/update/UpdatePacks/", version+".zip", updateView, length);
            DownloadStart.downloadstart("http://81.70.235.33:8088/GameServer/update/"+version+".zip",System.getProperty("user.dir")+"/update/UpdatePacks/",version+".zip");
            long end = System.currentTimeMillis();
            updateView.getTitle().setText("下载完成，耗时：" + (end - start) +" ms 开始解压"+version+".zip....");
            start = System.currentTimeMillis();
            Tool.unZip(new File(System.getProperty("user.dir")+"/update/UpdatePacks/"+version+".zip"), System.getProperty("user.dir")+"/update/UpdatePacks/"+version);
            end = System.currentTimeMillis();
            updateView.getTitle().setText("解压完成，耗时：" + (end - start) +" ms");
            updateView.getTitle().setText("开始更新"+version);
            String p = System.getProperty("user.dir")+"/update/UpdatePacks/"+version+"/";
            if (new File(p+"Update.jar").exists()){//引用jar
                Tool.loadJar(p+"Update.jar");
                Class<?> aClass = null;
                try {
                    aClass = Class.forName("cn.dahua.update.Dahua");
                    aClass.getDeclaredMethod("main", String[].class).invoke((Object)null, (Object)new String[]{version});
                } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }else if(new File(p+"Update.bat").exists()){//看来是批量处理 为了防止报毒，这个方法先不使用
                //Tool.run_cmd("cmd /c start "+System.getProperty("user.dir")+"\\LTCraft\\UpdatePacks\\"+version+"\\Update.bat");
            }else if (new File(p+"Update.json").exists()){//可扩展
                JSONObject jsonObject = Tool.StrintToJson(Tool.readToString(p + "Update.json"));
                Tool.parsing(jsonObject, version);
            }
            File file = new File(System.getProperty("user.dir")+"/update/version.json");
            PrintStream ps = null;
            try {
                ps = new PrintStream(new FileOutputStream(file));
                ps.println("{\"version\":\""+version+"\"}");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            System.out.println("流程1结束");
            return true;
        } catch (RuntimeException e) {

            e.printStackTrace();
            return false;
        }

    }
}
